home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / info-sys / www / tkhtml-2.3 / tkhtml-2 / tkHTML-2.3 / maintext.tcl < prev    next >
Encoding:
Text File  |  1995-02-06  |  19.6 KB  |  753 lines

  1. #############################################
  2. # Main text window, status window, and bindings
  3.  
  4. #############################################
  5. # build main text window
  6.  
  7. proc MkMainText {} {
  8.  
  9.     frame .textframe 
  10.     frame .textframe.vp
  11.     text .textframe.vp.text -relief sunken -bd 2 \
  12.         -yscrollcommand {.textframe.vp.mainscroll set} \
  13.         -wrap char -setgrid true
  14.  
  15.     scrollbar .textframe.vp.mainscroll -orient vertical \
  16.         -command mainscrollProc
  17.     pack .textframe -fill both -expand true
  18.     pack .textframe.vp -fill both -expand true
  19.     pack .textframe.vp.text -side right -fill both -expand true 
  20.     pack .textframe.vp.mainscroll -side left -pady 10 -fill y 
  21.  
  22.     MkMainTextBindings
  23. }
  24.  
  25. #############################################
  26. # build status line (filename/event)
  27.  
  28. proc MkMainStatus {} {
  29.     frame .textframe.stati
  30.     label .textframe.stati.statusl -text "Filename:" -width 9
  31.     label .textframe.stati.status -textvariable {filename} \
  32.         -relief groove -bd 2 -width 50
  33.     label .textframe.stati.eventl -text "Event:" -width 6
  34.     label .textframe.stati.event -textvariable {event} \
  35.         -relief groove -bd 2 -width 25
  36.  
  37.     pack .textframe.stati -side bottom -fill x -expand 0 -anchor s
  38.  
  39.  
  40.     pack .textframe.stati.statusl -side left -in .textframe.stati
  41.     pack .textframe.stati.status -side left -in .textframe.stati
  42.     pack .textframe.stati.eventl -side left -in .textframe.stati
  43.     pack .textframe.stati.event -side left -in .textframe.stati
  44. }
  45.  
  46. #############################################
  47. # main window scrolling procedure
  48.  
  49. proc mainscrollProc {index} {
  50.     .textframe.vp.mainscroll  config \
  51.         -command {.textframe.vp.text  yview }
  52. }
  53.  
  54. #############################################
  55. # keyboard selection
  56.  
  57. proc MoveSelect {w X Y x y} {
  58.         if { ![ IsSelected ] } {
  59.                 tk_textSelectTo $w insert
  60.     }
  61.     set sx [ $w index @$X,$Y ]
  62.     set sf [ $w index sel.first ]
  63.     set sl [ $w index sel.last ]
  64.     set i [ tk_textIndexCloser $w $sx $sf $sl ]
  65.     set j [ expr 1-$i ]
  66.     set x1 [ expr "$x*$i" ]
  67.     set x2 [ expr "$x*$j" ]
  68.     set y1 [ expr "$y*$i" ]
  69.     set y2 [ expr "$y*$j" ]
  70.     if { $x1 > 0 } { set sf [ $w index "$sf +$x1 c" ] }
  71.     if { $x1 < 0 } { set sf [ $w index "$sf  $x1 c" ] }
  72.     if { $x2 > 0 } { set sl [ $w index "$sl +$x2 c" ] }
  73.     if { $x2 < 0 } { set sl [ $w index "$sl  $x2 c" ] }
  74.     if { $y1 > 0 } { set sf [ $w index "$sf +$y1 l" ] }
  75.     if { $y1 < 0 } { set sf [ $w index "$sf  $y1 l" ] }
  76.     if { $y2 > 0 } { set sl [ $w index "$sl +$y2 l" ] }
  77.     if { $y2 < 0 } { set sl [ $w index "$sl  $y2 l" ] }
  78.     $w tag remove sel 0.0 end
  79.     $w tag add sel $sf $sl
  80.     
  81. }
  82.  
  83.  
  84.  
  85. #############################################
  86. # main window bindings
  87.  
  88. proc MkMainTextBindings {} {
  89.     global netscape
  90.     bind .textframe.vp.text <Button-3> {
  91.         selection clear .textframe.vp.text
  92.     }
  93.     bind .textframe.vp.text <Control-Key-C> {
  94.         global CUTBUFFER
  95.         set CUTBUFFER [selection get]
  96.     }
  97.     bind .textframe.vp.text <Control-Key-V> {
  98.         global CUTBUFFER
  99.         .textframe.vp.text insert insert $CUTBUFFER
  100.     }
  101.     bind .textframe.vp.text <Control-Key-X> {
  102.         global CUTBUFFER
  103.         set CUTBUFFER [selection get]
  104.         .textframe.vp.text delete sel.first sel.last
  105.     }
  106.     bind .textframe.vp.text <Control-Key-a> {
  107.         .textframe.vp.text mark set insert "insert linestart"
  108.     }
  109.     bind .textframe.vp.text <Control-Key-b> {
  110.         %W mark set insert insert-1c
  111.     }
  112.     bind .textframe.vp.text <Control-Key-c> {
  113.         global CUTBUFFER
  114.         set CUTBUFFER [selection get]
  115.     }
  116.     bind .textframe.vp.text <Control-Key-e> {
  117.         .textframe.vp.text mark set insert "insert lineend"
  118.     }
  119.     bind .textframe.vp.text <Control-Key-f> {
  120.         %W mark set insert insert+1c
  121.     }
  122.     bind .textframe.vp.text <Control-Key-k> {
  123.         if {[%W compare insert == "insert lineend"]} {
  124.             .textframe.vp.text delete insert "insert lineend +1c"
  125.         } else {
  126.             .textframe.vp.text delete insert "insert lineend"
  127.         }
  128.     }
  129.     bind .textframe.vp.text <Control-Key-n> {
  130.         %W mark set insert  "insert+1l"
  131.     }
  132.     bind .textframe.vp.text <Control-Key-p> {
  133.         %W mark set insert "insert-1l"
  134.     }
  135.     bind .textframe.vp.text <Control-Key-P> {
  136.         SaveForPreview
  137.     }
  138.     bind .textframe.vp.text <Control-Key-q> {
  139.         global filename
  140.         QuitDlg
  141.     }
  142.     bind .textframe.vp.text <Control-Key-s> {
  143.         global filename
  144.         SaveFileDlg SaveCmd
  145.     }
  146.     bind .textframe.vp.text <Control-Key-v> {
  147. #        global CUTBUFFER
  148. #        .textframe.vp.text insert insert $CUTBUFFER
  149.         .textframe.vp.text mark set insert "insert +24 lines"
  150.         %W yview -pickplace insert
  151.     }
  152.     bind .textframe.vp.text <Control-Key-y> {
  153.         .textframe.vp.text mark set insert "insert -24 lines"
  154.         %W yview -pickplace insert
  155.     }
  156.     bind .textframe.vp.text <Control-Key-x> {
  157.         global CUTBUFFER
  158.         if {[IsSelected] == 1} {
  159.             set CUTBUFFER [selection get]
  160.             .textframe.vp.text delete sel.first sel.last
  161.         }
  162.     }
  163.     bind .textframe.vp.text <Control-Key-S> {
  164.         SearchDlg
  165.     }
  166.     bind .textframe.vp.text <Control-Key-F> {
  167.         Search
  168.     }
  169.     bind .textframe.vp.text <Enter> {
  170.         focus .textframe.vp.text
  171.     }
  172.     bind .textframe.vp.text <Key-Delete> {
  173.         if {[IsSelected] == 0} {
  174.             tk_textBackspace %W
  175.             %W yview -pickplace insert
  176.         } else {
  177.             .textframe.vp.text delete sel.first sel.last
  178.         }
  179.     }
  180.     # thanks Heiko Jacobs <jacobs@ipf.bau-verm.uni-karlsruhe.de>
  181.     bind .textframe.vp.text <Key-BackSpace> {
  182.         if {[IsSelected] == 0} {
  183.             tk_textBackspace %W
  184.             %W yview -pickplace insert
  185.         } else {
  186.             .textframe.vp.text delete sel.first sel.last
  187.         }
  188.     }
  189.  
  190.     # delete whole entities, functions, strings, words...
  191.     bind .textframe.vp.text <Shift-Key-BackSpace> {
  192.         global CUTBUFFER
  193.         set x [ %W get insert-1c ]
  194.         if { $x == ";" } then {
  195.             set j [ %W index "insert linestart" ]
  196.             set l [ %W get $j insert-1c ]
  197.             set i [ string last "&" $l ]
  198.             if { $i < 0 } { tk_textBackspace %W } else {
  199.                 set j [ %W index "insert linestart +$i c" ]
  200.                 set CUTBUFFER [ %W get $j insert ]
  201.                 %W delete $j insert
  202.             }
  203.         } elseif { $x == ">" } then {
  204.             set j [ %W index "insert linestart" ]
  205.             set l [ %W get $j insert-1c ]
  206.             set i [ string last "<" $l ]
  207.             if { $i < 0 } { tk_textBackspace %W } else {
  208.                 set j [ %W index "insert linestart +$i c" ]
  209.                 set CUTBUFFER [ %W get $j insert ]
  210.                 %W delete $j insert
  211.             }
  212.         } elseif { $x == "\"" } then {
  213.             set j [ %W index "insert linestart" ]
  214.             set l [ %W get $j insert-1c ]
  215.             set i [ string last "\"" $l ]
  216.             if { $i < 0 } { tk_textBackspace %W } else {
  217.                 set j [ %W index "insert linestart +$i c" ]
  218.                 set CUTBUFFER [ %W get $j insert ]
  219.                 %W delete $j insert
  220.             }
  221.         } else {
  222.             set j [ %W index "insert-1c wordstart" ]
  223.             set CUTBUFFER [ %W get $j insert ]
  224.             %W delete $j insert
  225.         }
  226.         %W yview -pickplace insert
  227.     }
  228.     bind .textframe.vp.text <Shift-Key-Delete> {
  229.         global CUTBUFFER
  230.         set x [ %W get insert-1c ]
  231.         if { $x == ";" } then {
  232.             set j [ %W index "insert linestart" ]
  233.             set l [ %W get $j insert-1c ]
  234.             set i [ string last "&" $l ]
  235.             if { $i < 0 } { tk_textBackspace %W } else {
  236.                 set j [ %W index "insert linestart +$i c" ]
  237.                 set CUTBUFFER [ %W get $j insert ]
  238.                 %W delete $j insert
  239.             }
  240.         } elseif { $x == ">" } then {
  241.             set j [ %W index "insert linestart" ]
  242.             set l [ %W get $j insert-1c ]
  243.             set i [ string last "<" $l ]
  244.             if { $i < 0 } { tk_textBackspace %W } else {
  245.                 set j [ %W index "insert linestart +$i c" ]
  246.                 set CUTBUFFER [ %W get $j insert ]
  247.                 %W delete $j insert
  248.             }
  249.         } elseif { $x == "\"" } then {
  250.             set j [ %W index "insert linestart" ]
  251.             set l [ %W get $j insert-1c ]
  252.             set i [ string last "\"" $l ]
  253.             if { $i < 0 } { tk_textBackspace %W } else {
  254.                 set j [ %W index "insert linestart +$i c" ]
  255.                 set CUTBUFFER [ %W get $j insert ]
  256.                 %W delete $j insert
  257.             }
  258.         } else {
  259.             set j [ %W index "insert-1c wordstart" ]
  260.             set CUTBUFFER [ %W get $j insert ]
  261.             %W delete $j insert
  262.         }
  263.     }
  264.     # split line and move to start of next line (removed for now)
  265.     bind .textframe.vp.text <Key-Return> {
  266.         %W insert insert "\n"
  267. #        %W mark set insert "insert +1l linestart"
  268.         %W yview -pickplace insert
  269.     }
  270.     bind .textframe.vp.text <Key-Down> {
  271.         %W mark set insert insert+1l
  272.         %W yview -pickplace insert
  273.     }
  274.     bind .textframe.vp.text <Key-End> {
  275.         %W mark set insert "insert lineend"
  276.         %W yview -pickplace insert
  277.     }
  278.     bind .textframe.vp.text <Control-End> {
  279.         %W mark set insert end
  280.         %W yview -pickplace insert
  281.     }
  282.     bind .textframe.vp.text <Key-Home> {
  283.         %W mark set insert "insert linestart"
  284.         %W yview -pickplace insert
  285.     }
  286.     bind .textframe.vp.text <Control-Home> {
  287.         %W mark set insert 1.0
  288.         %W yview -pickplace insert
  289.     } 
  290.     bind .textframe.vp.text <Key-Left> {
  291.         %W mark set insert insert-1c
  292.         %W yview -pickplace insert
  293.     }
  294.     bind .textframe.vp.text <Key-Right> {
  295.         %W mark set insert insert+1c
  296.         %W yview -pickplace insert
  297.     }
  298.     bind .textframe.vp.text <Key-Up> {
  299.         %W mark set insert insert-1l
  300.         %W yview -pickplace insert
  301.     }
  302.     if {$netscape == 1} {
  303.         bind .textframe.vp.text <Mod1-Key-C> {
  304.             DoFormat <CENTER>\n </CENTER>\n
  305.         }
  306.     }
  307.     bind .textframe.vp.text <Mod1-Key-1> {DoFormat <H1> </H1>}
  308.     bind .textframe.vp.text <Mod1-Key-2> {DoFormat <H2> </H2>}
  309.     bind .textframe.vp.text <Mod1-Key-3> {DoFormat <H3> </H3>}
  310.     bind .textframe.vp.text <Mod1-Key-4> {DoFormat <H4> </H4>}
  311.     bind .textframe.vp.text <Mod1-Key-5> {DoFormat <H5> </H5>}
  312.     bind .textframe.vp.text <Mod1-Key-6> {DoFormat <H6> </H6>}
  313.     bind .textframe.vp.text <Mod1-Key-C> {DoFormat "<!--" ">"}
  314.     bind .textframe.vp.text <Mod1-Key-H> {
  315.         if {[IsSelected] == 1} {
  316.             set temp [selection get]
  317.             set temp2 [GetUrl hyperlink]
  318.             if {[string length $temp2] != 0 } {
  319.                 .textframe.vp.text insert sel.first "<A HREF=\"$temp2\">$temp</A>"
  320.                 .textframe.vp.text delete sel.first sel.last
  321.             }
  322.         } else {
  323.             DoFormat "<A HREF=\"" "\">CHANGE_ME</A>"
  324.         }
  325.     }
  326.     bind .textframe.vp.text <Mod1-Key-I> {
  327.         if {[IsSelected] == 1} {
  328.             set temp [selection get]
  329.             .textframe.vp.text insert sel.first "<IMG SRC=\"$temp\">"
  330.             .textframe.vp.text delete sel.first sel.last
  331.         } else {
  332.             DoFormat "<IMG SRC=\"" "\">"
  333.         }
  334.     }
  335.     bind .textframe.vp.text <Mod1-Key-P> {DoFormat <PRE> </PRE>}
  336.     bind .textframe.vp.text <Mod1-Key-Return> {
  337.         .textframe.vp.text insert insert "<P>"
  338.     }
  339.     bind .textframe.vp.text <Mod1-Key-a> {DoFormat <ADDRESS> </ADDRESS>}
  340.     bind .textframe.vp.text <Mod1-Key-ampersand> {
  341.         .textframe.vp.text insert insert "&"
  342.     }
  343.     bind .textframe.vp.text <Mod1-Key-b> {DoFormat "<B>" "</B>"}
  344.     bind .textframe.vp.text <Mod1-Key-c> {DoFormat <CODE> </CODE>}
  345.     bind .textframe.vp.text <Mod1-Key-e> {DoFormat <EM> </EM>}
  346.     bind .textframe.vp.text <Mod1-Key-greater> {
  347.         .textframe.vp.text insert insert ">"
  348.     }
  349.     bind .textframe.vp.text <Mod1-Key-h> {
  350.         .textframe.vp.text insert insert "<HR>\n"
  351.     }
  352.     bind .textframe.vp.text <Mod1-Key-i> {DoFormat <I> </I>}
  353.     bind .textframe.vp.text <Mod1-Key-l> {
  354.         .textframe.vp.text insert insert "<LI>"
  355.     }
  356.     bind .textframe.vp.text <Mod1-Key-less> {
  357.         .textframe.vp.text insert insert "<"
  358.     }
  359.     bind .textframe.vp.text <Mod1-Key-p> {
  360.         .textframe.vp.text insert insert "<BR>"
  361.     }
  362.     bind .textframe.vp.text <Mod1-Key-quotedbl> {
  363.         .textframe.vp.text insert insert """
  364.     }
  365.     bind .textframe.vp.text <Mod1-Key-s> {DoFormat <STRONG> </STRONG>}
  366.     bind .textframe.vp.text <Mod1-Key-space> {
  367.         .textframe.vp.text insert insert " "
  368.     }
  369.     bind .textframe.vp.text <Mod1-Key-t> {DoFormat <TITLE> </TITLE>}
  370.     bind .textframe.vp.text <Mod1-Key-u> {DoFormat <U> </U>}
  371.     bind .textframe.vp.text <Shift-Left> {
  372.         MoveSelect %W %x %y -1 0
  373.     }
  374.     bind .textframe.vp.text <Shift-Right> {
  375.         MoveSelect %W %x %y 1 1
  376.     }
  377.     bind .textframe.vp.text <Shift-Down> {
  378.         MoveSelect %W %x %y 0 1
  379.     }
  380.     bind .textframe.vp.text <Shift-Up> {
  381.         MoveSelect %W %x %y 0 -1
  382.     }
  383. }
  384.  
  385. #############################################
  386. # check if there is something in selection
  387. # returns 1 if there is, 0 if selection is empty
  388.  
  389. proc IsSelected {} {
  390.     if {[selection own] != ""} {
  391.         if [catch {selection get} result] {
  392.             return 0
  393.         } else {
  394.             if {[string match *tagged* $result] == 0} {
  395.                 return 1
  396.             } else {
  397.                 return 1
  398.             }
  399.         }
  400.     } else {
  401.         return 0
  402.     }
  403. }
  404.  
  405.  
  406. #############################################
  407. # Create formatting tags in main window
  408. # if there is a selection, place it between the tags
  409. # if there is no selection, place the cursor between them
  410. #
  411. # open is the opening format command <X>
  412. # close is the clincher </X>
  413.  
  414. proc DoFormat {open close} {
  415.     if {[IsSelected] == 0} {
  416.         .textframe.vp.text insert insert "$open$close"
  417.         set length [string length $close]
  418.         .textframe.vp.text mark set insert "insert -$length c"
  419.     } else {
  420.         set temp [selection get]
  421.         .textframe.vp.text insert sel.first "$open$temp$close"
  422.         .textframe.vp.text delete sel.first sel.last
  423.     }
  424. }
  425.  
  426.  
  427. proc Search {} {
  428.  
  429.     global searchstring
  430.     global direction
  431.  
  432.     if {[winfo exists .search] == 1} {
  433.         destroy .search
  434.     }
  435.  
  436.     if {[string length $searchstring] == 0} {
  437.         return 0
  438.     }
  439.  
  440.     if {$direction == "for"} {
  441.         set lastfirst first
  442.         set textpart [.textframe.vp.text get insert end]
  443.         set countfrom insert
  444.     } else {
  445.         set lastfirst last
  446.         set textpart [.textframe.vp.text get 0.0 {insert -1char}]
  447.         set countfrom 0.0
  448.     }
  449.  
  450.     set foundpos [string $lastfirst $searchstring $textpart]
  451.  
  452.     if {$foundpos == -1} then {
  453.         GenericDialog "\"$searchstring\" not found."
  454.         return
  455.     }
  456.  
  457.     if {[IsSelected] == 1} {
  458.         if {[selection own] != ""} {
  459.             selection clear .textframe.vp.text
  460.         }
  461.     } 
  462.     
  463.     set lastpos [expr {$foundpos + [string length $searchstring]}]
  464.     .textframe.vp.text tag add sel \
  465.         "$countfrom + $foundpos chars" "$countfrom + $lastpos chars"
  466.  
  467.     .textframe.vp.text mark set insert "$countfrom + $lastpos chars"
  468.     .textframe.vp.text yview -pickplace insert
  469. }
  470.  
  471. proc SearchDlg {} {
  472.  
  473.     global searchstring direction searchstate
  474.  
  475.     ClearEvent "Search..."
  476.  
  477.     set foo ""
  478.     set string ""
  479.  
  480.     toplevel .search
  481.     wm title .search "Search"
  482.     set x [expr 275 + [winfo x .]]
  483.     set y [expr 140 + [winfo y .]]
  484.     wm geometry .search +$x+$y
  485.  
  486.     frame .search.top -relief raised -bd 1
  487.     label .search.top.lbl -text "Enter the search string"
  488.     entry .search.top.entry -textvariable foo
  489.  
  490.     radiobutton .search.top.forward -text "Forward" -variable direction \
  491.         -value for -relief flat -command {set direction "for"}
  492.     radiobutton .search.top.backward -text "Backward" \
  493.         -variable direction \
  494.         -value back -relief flat -command {set direction "back"}
  495.  
  496.     tixDlgBtns .search.btns
  497.     .search.btns add ok -text "Ok" -width 8
  498.     .search.btns add cancel -text "Cancel" -width 8
  499.  
  500.     bind .search <Enter> {
  501.         focus .search.top.entry
  502.     }
  503.  
  504.     bind .search.top.entry <Key-Return> { 
  505.         set searchstring $foo
  506.         Search
  507.     }
  508.     pack .search.top.lbl -expand yes -fill x -padx 10 -pady 10
  509.     pack .search.top.entry -expand yes -fill x -padx 10 -pady 10
  510.     pack .search.top.forward -side left
  511.     pack .search.top.backward -side right
  512.     pack .search.top -expand yes -fill both
  513.     pack .search.btns -fill x
  514.  
  515.     .search.btns button ok config -command {
  516.         set searchstring $foo
  517.         Search
  518.     }
  519.  
  520.     .search.btns button cancel config -command {
  521.         destroy .search
  522.     }
  523.  
  524. }
  525.  
  526. proc GenericDialog {message} {
  527.  
  528.     if {[winfo exists .message] == 1} {
  529.         destroy .message
  530.     }
  531.     toplevel .message
  532.     wm title .message "Notice"
  533.  
  534. #    tk_dialog .message "Notice" "$message" warning 0 "Ok"
  535.  
  536.     set x [expr 255 + [winfo x .]]
  537.     set y [expr 150 + [winfo y .]]
  538.     wm geometry .message +$x+$y
  539.     
  540.     frame .message.top -relief raised -bd 1
  541.     pack .message.top -side top -fill both -expand true
  542.     frame .message.bot -relief raised -bd 1
  543.     pack .message.bot -side bottom -fill both -expand true
  544.     
  545.     message .message.top.msg -text $message -width 200 \
  546.         -font "-adobe-helvetica-bold-r-normal-*-14-*-*-*-*-*-*-*"
  547.     pack .message.top.msg -side right -expand 1 -fill both \
  548.         -padx 5m -pady 5m 
  549.     
  550.     button .message.bot.ok -text "Close" -command {destroy .message} 
  551.     pack .message.bot.ok -padx 5m -pady 5m
  552. }
  553.  
  554. proc SearchReplaceDlg {} {
  555.  
  556.     global searchstring direction searchstate confirm
  557.  
  558.     ClearEvent "Search and Replace..."
  559.  
  560.     set foo ""
  561.     set string ""
  562.  
  563.     toplevel .searchr
  564.     wm title .searchr "Search"
  565.     set x [expr 250 + [winfo x .]]
  566.     set y [expr 140 + [winfo y .]]
  567.     wm geometry .searchr +$x+$y
  568.  
  569.     frame .searchr.top -relief raised -bd 1
  570.     label .searchr.top.lbl -text "Search string"
  571.     entry .searchr.top.entry -textvariable foo
  572.     label .searchr.top.lbl2 -text "Replacement string"
  573.     entry .searchr.top.entry2 -textvariable foo2
  574.  
  575.     frame .searchr.top.dir -relief groove -bd 1
  576.     radiobutton .searchr.top.dir.forward -text "Forward" \
  577.         -variable direction \
  578.         -value for -relief flat -command {set direction "for"}
  579.     radiobutton .searchr.top.dir.backward -text "Backward" \
  580.         -variable direction \
  581.         -value back -relief flat -command {set direction "back"}
  582.  
  583.     checkbutton .searchr.top.query -text "Confirm?" -variable confirm \
  584.         -relief flat
  585.  
  586.     tixDlgBtns .searchr.btns
  587.     .searchr.btns add ok -text "Ok" -width 8
  588.     .searchr.btns add cancel -text "Cancel" -width 8
  589.  
  590.     bind .searchr <Enter> {
  591.         focus .searchr.top.entry
  592.     }
  593.  
  594.     bind .searchr.top.entry <Key-Tab> {
  595.         focus .searchr.top.entry2
  596.     }
  597.     bind .searchr.top.entry2 <Key-Tab> {
  598.         focus .searchr.top.entry
  599.     }
  600.  
  601.     bind .searchr.top.entry <Key-Return> { 
  602.         set searchstring $foo
  603.         set replacestring $foo2
  604.         SearchReplace
  605.     }
  606.     bind .searchr.top.entry2 <Key-Return> {
  607.         set searchstring $foo
  608.         set replacestring $foo2
  609.         SearchReplace
  610.     }
  611.  
  612.     pack .searchr.top.lbl -expand yes -fill x -padx 10
  613.     pack .searchr.top.entry -expand yes -fill x -padx 10 -pady 10
  614.     pack .searchr.top.lbl2 -expand yes -fill x -padx 10
  615.     pack .searchr.top.entry2 -expand yes -fill x -padx 10 -pady 10
  616.     pack .searchr.top.query -side bottom
  617.     pack .searchr.top.dir -side bottom -pady 10 -padx 10
  618.     pack .searchr.top.dir.forward -side left
  619.     pack .searchr.top.dir.backward -side right
  620.     pack .searchr.top -expand yes -fill both
  621.     pack .searchr.btns -fill x
  622.  
  623.     .searchr.btns button ok config -command {
  624.         set searchstring $foo
  625.         set replacestring $foo2
  626.         SearchReplace
  627.     }
  628.  
  629.     .searchr.btns button cancel config -command {
  630.         destroy .searchr
  631.         return
  632.     }
  633.  
  634. }
  635.  
  636. proc SearchReplace {} {
  637.  
  638.     global searchstring direction searchstate confirm replacestring
  639.     global confirmopt
  640.  
  641.     destroy .searchr
  642.  
  643.     ClearEvent "Search and replace..."
  644.  
  645.     if {[string length $searchstring] == 0} {
  646.         return 0
  647.     }
  648.  
  649.     set foundpos 1
  650.  
  651.     while {$foundpos != -1} {
  652.  
  653.     if {[IsSelected] == 1} {
  654.         if {[selection own] != ""} {
  655.             selection clear .textframe.vp.text
  656.         }
  657.     } 
  658.  
  659.  
  660.     if {$direction == "for"} {
  661.         set lastfirst first
  662.         set textpart [.textframe.vp.text get insert end]
  663.         set countfrom insert
  664.     } else {
  665.         set lastfirst last
  666.         set textpart [.textframe.vp.text get 0.0 {insert -1char}]
  667.         set countfrom 0.0
  668.     }
  669.  
  670.     set foundpos [string $lastfirst $searchstring $textpart]
  671.  
  672.  
  673.     if {$foundpos != -1} then {
  674.         set lastpos [expr {$foundpos + [string length $searchstring]}]
  675.         .textframe.vp.text tag add sel \
  676.             "$countfrom + $foundpos chars" "$countfrom + $lastpos chars"    
  677.  
  678.         .textframe.vp.text mark set insert "$countfrom + $lastpos chars"
  679.         .textframe.vp.text yview -pickplace insert
  680.  
  681.         if {$confirm != 1} {
  682.             .textframe.vp.text del sel.first sel.last
  683.             .textframe.vp.text insert insert $replacestring
  684.         } else {
  685.             ConfirmDlg
  686.             tkwait window .confirm
  687.             if {$confirmopt == 1} {
  688.                 .textframe.vp.text del sel.first sel.last
  689.                 .textframe.vp.text insert insert $replacestring
  690.             } elseif {$confirmopt == 0} {
  691.                 # skip
  692.             } else {
  693.                 if {[IsSelected] == 1} {
  694.                     selection clear .textframe.vp.text
  695.                 }
  696.                 return
  697.             }
  698.         
  699.         }
  700.     }
  701.  
  702.     }
  703. }
  704.  
  705. proc ConfirmDlg {} {
  706.     global searchstring replacestring confirmoptt
  707.  
  708.     if [winfo exists .confirm] {
  709.         return
  710.     }
  711.  
  712.     toplevel .confirm
  713.  
  714.     wm title .confirm "Confirm"
  715.  
  716.     set x [expr 420 + [winfo x .]]
  717.     set y [expr 50 + [winfo y .]]
  718.  
  719.     wm geometry .confirm +$x+$y
  720.  
  721.     frame .confirm.top -relief raised -border 1
  722.     message .confirm.top.msg \
  723.         -font -*-helvetica-bold-r-normal-*-14-*-*-*-*-*-*-* \
  724.         -relief sunken -bd 1 -anchor n -padx 40 -width 370\
  725.         -text "Replace \"$searchstring\" with \"$replacestring\"?"
  726.  
  727.     tixDlgBtns .confirm.btns
  728.     .confirm.btns add replace -text "Replace"   -width 8
  729.     .confirm.btns add ignore  -text "Ignore" -width 8
  730.     .confirm.btns add cancel -text "Cancel" -width 8
  731.  
  732.     pack .confirm.top.msg -expand yes -fill both -padx 10 -pady 10
  733.     pack .confirm.top -expand yes -fill both 
  734.     pack .confirm.btns -fill x
  735.  
  736.     .confirm.btns button replace config -command {
  737.         set confirmopt 1
  738.         destroy .confirm
  739.     }
  740.     .confirm.btns button ignore  config -command {
  741.         set confirmopt 0
  742.         destroy .confirm
  743.     }
  744.     .confirm.btns button cancel config -command {
  745.         set confirmopt -1
  746.         destroy .confirm
  747.     }
  748.  
  749. }
  750.  
  751.  
  752.  
  753.